<template>
  <div class="app-container">
    <table-pro
      :isShowIdColumn="false"
      idColumnName="id"
      :columns="columns"
      dialog-title="证书"
      :table-request-fn="listCert"
      :create-fn="addCert"
      :update-fn="updateCert"
      :delete-fn="delCert"
      :form-params="{ cerCategory: '1' }"
      :defaultQueryParams="{companyId: +$route.query.companyId}"
      :beforeSubmitCallback="beforeSubmitCallback"
    >
    <template #toolbar>
      <el-col :span="21" style="text-align: right;">
        <el-button
          v-for="(item,index) in countObj"
          :key="index"
          style="pointer-events: none;"
          size="mini"
          >{{selectDictLabel(dict.type.cer_class, item.name)}}：{{item.num}}</el-button>
      </el-col>
    </template>
  </table-pro>
  </div>
</template>

<script>
import {
  listCert,
  getCert,
  addCert,
  updateCert,
  delCert,
  getCertCount
} from "@/api/cert/index.js";

export default {
  name: "Expire",
  dicts: ["sys_normal_disable", "cert_company", "cert_type","cer_state","cer_from","cer_class","cer_category"],
  data() {
    return {
      personal: true,
      countObj:[]

    };
  },
  computed: {
    columns() {
      return [
        {
          title: "证书分类",
          field: "cerCategory",
          search: true,
          type: "radio",
          add: true,
          change: this.onCertTypeChange,
          options: this.dict.type.cer_category,
          disabled: true,
        },
        {
          title: "持证人",
          field: "cerLicensee",
          add: this.personal ? [{ required: true, message: "请输入" }] : false,
          search: true,
          hidden:true
        },
        {
          title: "身份证号",
          field: "idNumbers",
          add: this.personal ? [{ required: true, message: "请输入" }] : false,
          search: true,
          hidden:true
        },
        {
          title: "证书类型",
          field: "cerClass",
          add: [{ required: true, message: "请输入" }],
          search: true,
          type: "select",
          options: this.dict.type.cer_class,
        },
        {
          title: "证书形式",
          field: "cerFrom",
          add: [{ required: true, message: "请输入" }],
          type: "select",
          options: this.dict.type.cer_from,
        },
        {
          title: "证书编号",
          field: "cerNumber",
          add: [{ required: true, message: "请输入" }],
          add: true,
          hidden:true
        },
        {
          title: "证书名称",
          field: "cerName",
          add: [{ required: true, message: "请输入" }],
          add: true,
        },
        {
          title: "发证单位",
          field: "cerUnit",
          add: true,
        },
        {
          title: "发证时间",
          field: "cerStartTime",
          type: "date",
          add: true,
        },
        {
          title: "过期时间",
          field: "cerValidity",
          type: "date",
          add: true,
          hidden:true
        },
        {
          title: "过期时间",
          field: "cerValidity",
          type: "daterange",
          search: true,
          hidden:true
        },
        {
          title: "证书状态",
          field: "cerState",
          add: true,
          search: true,
          type: "select",
          options: this.dict.type.cer_state,
        },
        // {
        //   title: "证书性质",
        //   field: "status8",
        //   add: true,
        //   search: true,
        //   type: "select",
        //   options: this.dict.type.cer_from,
        // },
        {
          title: "注册单位",
          field: "companyId",
          add: true,
          // search: true,
          type: "select",
          options: this.dict.type.cert_company,
          formProps: { multiple: true },
          hidden:true
        },
        {
          title: "联合著作人",
          field: "cerOwner",
          add: !this.personal,
          // search: true,
          hidden:true
        },
        // {
        //   title: "转入时间",
        //   field: "date11",
        //   type: "date",
        //   add: true,
        // },
        {
          title: "年检时间",
          field: "annuallTime",
          type: "date",
          add: true,
          hidden:true
        },
        {
          title: "证书存放地",
          field: "cerStore",
          add: true,
          hidden:true
        },
        {
          title: "保管人",
          field: "cerKeeper",
          type:"user",
          hidden:true
        },
        {
          title: "保管人",
          field: "cerKeeperId",
          hidden:true,
          add:true,
          type:"user"
        },
        {
          title: "保管部门",
          field: "dept",
          type:"department",
          hidden:true
        },
        {
          title: "保管部门",
          field: "deptId",
          hidden:true,
          add:true,
          type:"department"
        },
        {
          title: "备注",
          field: "remark",
          add: true,
          hidden:true,
          formProps: { type: "textarea" },
        },
        // {
        //   title: "附件",
        //   field: "attachments",
        //   add: true,
        //   type: "img",
        //   render:(item)=>{
        //     if(item.attachments){
        //       return item.attachments.split(",")
        //     }else{
        //       return []
        //     }
        //   }
        // },
        {
          title: "附件",
          field: "attachments",
          add: true,
          type: "file",
          render:(item)=>{
            if(item.attachments){
              // 附件预览 如果附件有图片，就预览图片，否则 就显示查看附件
              return item.attachments.split(",").filter((item)=>{
                return /\.(jpg|jpeg|png|gif|bmp)$/.test(item)
              })
            }else{
              return []
            }
          }
        },
        {
          title: "附件数量",
          field: "attachments",
          render:(item)=>{
            return item.attachments.split(",").length
          }
        },
        {
          actions: [
            { type: "edit", beforeCallback: this.onBeforeEditCallback },
            { type: "delete" },{ type: "detail",beforeCallback: this.onBeforeEditCallback },
          ],
        },
      ];
    },
  },
  created() {
    getCertCount({companyId: +this.$route.query.companyId}).then((res)=>{
      this.countObj = res.data
    })
  },
  methods: {
    listCert:listCert,
    getCert:getCert,
    addCert:addCert,
    updateCert:updateCert,
    delCert:delCert,
    onCertTypeChange(val) {
      this.personal = val == 1;
    },
    onBeforeEditCallback(item) {
      this.personal = item.cerCategory == '1';
      if(item.companyId){
        item.companyId = item.companyId.toString().split(',')
      }
      return item;
    },
    beforeSubmitCallback(form){
      if(form.companyId){
        form.companyId = form.companyId.toString();
      }
      return form
    }
  },
};
</script>
